﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Framework.Platform.Components.UI.Presenter;

namespace Framework.Platform.Components.UI.View
{
    public partial class SimpleDetailModuleView : BaseModuleView
    {
        ToolStrip toolStrip = null;

        ToolStrip editModeToolStrip = null;

        private ViewCommander _CurrentViewCommander = null;

        public SimpleDetailModuleView()
        {
            InitializeComponent();
        }

        protected void InitCommanders()
        {
            ToolStripButton commanderButton = null;
            if (editModeToolStrip == null)
            {
                this.editModeToolStrip = new ToolStrip();
                this.editModeToolStrip.Location = new System.Drawing.Point(0, 0);
                this.editModeToolStrip.Name = "editModeToolStrip";
                this.editModeToolStrip.TabIndex = 1;
                this.editModeToolStrip.Text = "editModeToolStrip";
                this.editModeToolStrip.Visible = false;
                this.Controls.Add(this.editModeToolStrip);

                foreach (ViewCommander commander in this.EditModeCommanders)
                {
                    commanderButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
                    commanderButton.Name = commander.CommanderKey;
                    commanderButton.AutoSize = true;
                    commanderButton.Text = commander.CommanderName;
                    commanderButton.Click += new EventHandler(submitButton_Click);
                    editModeToolStrip.Items.Add(commanderButton);
                }
            }

            if (toolStrip == null)
            {
                this.toolStrip = new ToolStrip();
                this.toolStrip.Location = new System.Drawing.Point(0, 0);
                this.toolStrip.Name = "toolStrip";
                this.toolStrip.TabIndex = 0;
                this.toolStrip.Text = "toolStrip";
                foreach (ViewCommander commander in this.GetViewCommanders())
                {
                    commanderButton = new ToolStripButton();
                    commanderButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
                    //toolStripDropDownButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripDropDownButton1.Image")));
                    //toolStripDropDownButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                    commanderButton.Name = commander.CommanderKey;
                    commanderButton.AutoSize = true;
                    commanderButton.Text = commander.CommanderName;
                    if (!string.IsNullOrEmpty(commander.Description))
                    {
                        commanderButton.ToolTipText = commander.Description;
                    }
                    commanderButton.Click += new EventHandler(commanderButton_Click);
                    commanderButton.Tag = commander;
                    toolStrip.Items.Add(commanderButton);
                }
                this.Controls.Add(this.toolStrip);
            }
        }

        void cancelButton_Click(object sender, EventArgs e)
        {
            this.SetViewMode();
        }

        void submitButton_Click(object sender, EventArgs e)
        {
            ViewCommanderEventArgs arg = new ViewCommanderEventArgs();
            arg.Commander = this._CurrentViewCommander;
            arg.ViewEventKey = this._CurrentViewCommander.CommanderKey + "_Submit";
            this.OnViewEventRaise(arg);
        }

        void commanderButton_Click(object sender, EventArgs e)
        {
            ViewCommander commander = (ViewCommander)((ToolStripButton)sender).Tag;
            if (commander != null)
            {
                this._CurrentViewCommander = commander;
                switch (commander.CommanderType)
                {
                    case CommanderType.ForEdit:
                        this.SetEditMode();
                        break;
                    case CommanderType.ForNew:
                        this.SetEditMode();
                        break;
                    case CommanderType.ForOperation:
                        ViewCommanderEventArgs arg = new ViewCommanderEventArgs();
                        arg.Commander = this._CurrentViewCommander;
                        arg.ViewEventKey = this._CurrentViewCommander.CommanderKey;
                        this.OnViewEventRaise(arg);
                        this._CurrentViewCommander = commander;
                        break;
                    default:
                        break;
                }
            }
        }

        private void SetEditMode()
        {
            this.toolStrip.Visible = false;
            this.editModeToolStrip.Visible = true;
            if (this._CurrentViewCommander.CommanderType == CommanderType.ForEdit)
            {
            }
            else if (this._CurrentViewCommander.CommanderType == CommanderType.ForNew)
            {
            }
        }

        private void SetViewMode()
        {
            this.toolStrip.Visible = true;
            this.editModeToolStrip.Visible = false;

        }

        private void SimpleDetailModuleView_Load(object sender, EventArgs e)
        {
            this.InitCommanders();
        }
    }
}
 